package com.qxl.forest.business.controller;

import com.qxl.forest.core.annotion.BussinessLog;
import com.qxl.forest.core.annotion.Permission;
import com.qxl.forest.common.constants.RequestParams;
import com.qxl.forest.business.persistence.dao.DeptDao;
import com.qxl.forest.business.persistence.entity.Dept;
import com.qxl.forest.common.util.ToolUtil;
import com.qxl.forest.exception.BizExceptionEnum;
import com.qxl.forest.exception.BussinessException;
import com.qxl.forest.util.CommRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: qiuxinlin
 * @Dercription: 部门控制器
 * @Date: 2017/12/7
 */
@RestController
@RequestMapping("/dept")
public class DeptController extends BaseController {

    @Autowired
    DeptDao deptDao;

    /**
     * 获取所有部门列表
     *
     * @param requestParams
     * @return
     */
    @PostMapping(value = "/get")
    @Permission
    public CommRes getAll(@RequestBody RequestParams requestParams) {
        if (requestParams.getPageSize() == 0) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        PageRequest pageRequest = new PageRequest(requestParams.getPageNumber(), requestParams.getPageSize());
        Page depts = deptDao.findAll(pageRequest);
        return CommRes.success(depts);
    }

    /**
     * 新增部门
     *
     * @param dept
     * @return
     */
    @PostMapping(value = "/add")
    @Permission
    @BussinessLog(value = "添加部门", key = "dept")
    public CommRes add(@RequestBody Dept dept) {
        //完善pids,根据pid拿到pid的pids
        deptSetPids(dept);
        deptDao.save(dept);
        return CommRes.successRes();
    }

    /**
     * 修改部门
     *
     * @param dept
     * @return
     */
    @PutMapping(value = "/update")
    @Permission
    @BussinessLog(value = "修改部门", key = "dept")
    public CommRes update(@RequestBody Dept dept) {
        deptSetPids(dept);
        deptDao.save(dept);
        return CommRes.successRes();
    }

    /**
     * 删除部门
     *
     * @param deptId
     * @return
     */
    @DeleteMapping(value = "/delete/{deptId}")
    @Permission
    @BussinessLog(value = "删除部门", key = "deptId")
    public CommRes delete(@PathVariable Integer deptId) {
        deptDao.delete(deptId);
        return CommRes.successRes();
    }

    /**
     * 获取部门树形列表 通过elementUi的tree组件的懒加载实现
     *
     * @param pid
     * @return
     */
    @GetMapping(value = "/selectDeptTreeList/{pid}")
    @Permission
    public CommRes selectDeptTreeList(@PathVariable Integer pid) {
        List depts = deptDao.findByPid(pid);
        return CommRes.success(depts);
    }

    /**
     * 根据当前部门获取上级部门的pids 并设置当前部门的pids
     *
     * @param dept
     */
    private void deptSetPids(Dept dept) {
        if (ToolUtil.isEmpty(dept.getPid()) || dept.getPid().equals(0)) {
            dept.setPid(0);
            dept.setPids("[0],");
        } else {
            int pid = dept.getPid();
            Dept temp = deptDao.findOne(pid);
            String pids = temp.getPids();
            dept.setPid(pid);
            dept.setPids(pids + "[" + pid + "],");
        }
    }
}
